package com.itheima.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @author robin
 * @version 1.0
 * AbstractAnnotationConfigDispatcherServletInitializer: 对web容器进行初始化，tomcat服务器就可以加载
 */
public class DispatcherServletInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    //加载spring的配置文件，由于这里没有和spring框架整合，没有spring配置文件
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    //对springmvc配置文件加载，进而springmvc Servlet进行实例化
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringmvcConfig.class};
    }

    //springmvc DispatcherServlet 配置成”/“,可以对所有请求进行处理响应
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //spring提供的处理乱码的过滤器,对post请求乱码处理
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        //强制对request请求数据和response响应结果进行编码
        characterEncodingFilter.setForceEncoding(true);
        return new Filter[]{characterEncodingFilter};
    }
}
